<?php
//预售接口
class inman_api_presale extends inman_api_base {
    public function __construct($app){
        $this->app=$app;
        $this->db = kernel::database();
        $this->goodsModel=kernel::single('b2c_mdl_goods');;
        $this->productsModel=kernel::single('b2c_mdl_products');
        $this->prepareModel=kernel::single('preparesell_mdl_preparesell');
        $this->prepareGoodModel=kernel::single('preparesell_mdl_preparesell_goods');
    }
    //预售商品列表
    public function index($params,&$service){
        //取预售订金结束时间大于当前的时间的 且必须启用
        $filter=array('filter_sql'=>'end_time>'.time(),'status'=>'true');
    	$prepareGoods=$this->prepareModel->getList('*',$filter);
    	if(!$prepareGoods){
    		$return['product_list']='';
    		return $return;
    	}
    	foreach ($prepareGoods as $key => $prepareGood) {
            //获取商品信息
    		$good=$this->goodsModel->getRow('bn,image_default_id',array('goods_id'=>$prepareGood['goods_id']));
            //获取预售商品的一件货品（取的是第一条货品）
            $prepareProduct=$this->prepareGoodModel->getRow('*',array('prepare_id'=>$prepareGood['prepare_id']));
    		$data[$key]['goods_sn']=$good['bn'];
    		$data[$key]['deposit']=$prepareProduct['preparesell_price'];
    		$data[$key]['promo_price']=$prepareProduct['promotion_price'];
    		$data[$key]['balance']=$prepareProduct['promotion_price']-$prepareProduct['preparesell_price'];
    		$data[$key]['participants']=$this->getJoinNumber($prepareGood['prepare_id']);
    		$data[$key]['img_url']=base_storager::image_path($good['image_default_id']);//原图路径;
    		$data[$key]['rule_html']=$prepareGood['description'];
    		$data[$key]['head_bg']='未知';
    		
    	}
    	$return['product_list']=$data;
    	return $return;

    }
    public function settle($params,&$service){
        //检测参数
        if(isset($params['sid'])&&$params['sid']!=null){
            $member_id=$this->check_sid($params['sid']);
            if(!$member_id){
                return $service->send_user_error('100001','sid fail');
            }else{
                if(!isset($params['sku_sn'])||$params['sku_sn']==null){
                    $error['status']='false';
                    $error['message']=$this->app->_('参数不正确');
                    return $error;
                }
                //$number=intval($params['goods_number']);
                //return $number;
                if(!isset($params['goods_number'])||!is_numeric($params['goods_number'])){
                    $error['status']='false';
                    $error['message']=$this->app->_('参数不正确');
                    return $error;
                }
                //检查货品是否存在
                $product=$this->productsModel->getRow('product_id',array('bn'=>$params['sku_sn']));
                if(!$product){
                     $error['status']='false';
                    $error['message']=$this->app->_('货品编号不存在');
                    return $error;
                }
                //获取货品和商品信息
                $product=$this->productsModel->getRow('*',array('bn'=>$params['sku_sn']));
                $good=$this->goodsModel->getRow('goods_id,bn,name',array('goods_id'=>$product['goods_id']));
                //获取货品的预售信息
                $prepare=$this->prepareGoodModel->getRow('preparesell_price,promotion_price',array('product_id'=>$product['product_id']));
                if(!$prepare){
                    $error['status']='false';
                    $error['message']=$this->app->_('该货品不是预售货品');
                    return $error;
                }
                //
                //$images=$this->getProductImagesByProId($product['product_id']);
                //货品的颜色、尺寸
                $color=$this->getSpecByProductId($product['product_id'],'颜色');
                $size=$this->getSpecByProductId($product['product_id'],'尺码');
                //组装返回的数据
                $return['sku_sn']=$params['sku_sn'];
                $return['goods_number']=intval($params['goods_number']);
                $return['goods_sn']=$good['bn'];
                $return['goods_name']=$good['name'];
                $return['color_code']=$color?$color['code']:'';
                $return['color_name']=$color?$color['name']:'';
                $return['size_name']=$size?$size['name']:'';
                $return['img_url']='图片';//$images[0];//取第一张图片
                $return['shipping_fee']='邮费未知';
                $return['deposit']=$prepare['preparesell_price'];
                $return['promo_price']=$prepare['promotion_price'];
                $return['balance']=$prepare['promotion_price']-$prepare['preparesell_price'];
                return $return;
            }
        }else{
            $error['status']='false';
            $error['message']=$this->app->_('参数不正确');
            return $error;
        }
    }
    //统计当前预售商品的参与人数
    private function getJoinNumber($prepare_id){
        //获得当前所有的预售的订单
        $prepareOrderModel=kernel::single('preparesell_mdl_prepare_order');
        //$count=$prepareOrderModel->count(array('prepare_id'=>$prepare_id));
        $sql="SELECT id FROM `sdb_preparesell_prepare_order` WHERE prepare_id={$prepare_id} group by member_id";
       $count=$this->db->select($sql);
       return count($count);//按照订单的数目统计
    }
    //根据货品id获取颜色名称、编码
    private function getSpecByProductId($id,$name='颜色'){
    	$product=$this->productsModel->getRow('*',array('product_id'=>$id));
    	$colors=$this->getAllSpecs($name);
    	if(!$colors){
    		return ;
    	}
    	if($product['spec_desc']){
    		//颜色\、尺寸规格
    		foreach ($product['spec_desc']['spec_value_id'] as $value) {
    			if(in_array($value, $colors)){
    				$name=kernel::single('b2c_mdl_spec_values')->getRow('spec_value,spec_value_id',array('spec_value_id'=>$value));
    				$return['code']=$name['spec_value_id'];//暂时以id作为编码
    				$return['name']=$name['spec_value'];
    				return $return;
    			}
    		}
    	}else{
    		return ;
    	}
    }
    //获得指定规格的所有属性值(默认属性为‘颜色’)
    public function getAllSpecs($name='颜色'){
    	$spec_value_ids=kernel::single('b2c_mdl_specification')->getRow('spec_id',array('spec_name'=>$name));
    	if(!$spec_value_ids){
    		return ;
    	}
    	$spec_type_id=$spec_value_ids['spec_id'];
    	$specs=kernel::single('b2c_mdl_spec_values')->getList('spec_value_id',array('spec_id'=>$spec_type_id));
    	if(!$specs){
    		return ;
    	}
    	return $this->changeArr($specs);
    }
    private function changeArr($arrs){
    	if(is_array($arrs)){
    		foreach ($arrs as $arr) {
    			foreach ($arr as $item) {
    				$return[]=$item;
    			}
    		}
    		return $return;
    	}else{
    		return $arrs;
    	}
    }
    
    
    //根据货品的id获取货品图片
    private function getImagesByProductId($id){
    	$product=$this->productsModel->getRow('spec_desc',array('product_id'=>$id));
    	$good=$this->goodsModel->getRow('spec_desc',array());
    }
}